# -*- coding: utf-8 -*-
import pandas as pd

from core.dataClasses import StockTradeDataColumnName, TacticResultData
from core.enums import Period, EventType
from core.tactic import Tactic
from functions import *
from core import logger


class TradeMa5Tactic(Tactic):
    classInfo = "5日均价线策略"

    def __init__(self, noticeclassify: str, sourceClassify: str):
        super(TradeMa5Tactic, self).__init__()
        self.noticeclassify: str = noticeclassify
        self.sourceClassify: str = sourceClassify

    def compute(self) -> bool:
        self.setPeriod(self.period)
        clo = close()
        rtq: pd.DataFrame = realtimeQuote()
        if rtq is None or rtq.empty:
            return False
        clo[rtq.index[0]] = rtq[StockTradeDataColumnName.CLOSE.value].values[0]
        ma5 = sma(clo[-15:], 5)
        ma10 = sma(clo[-15:], 10)

        judgment = clo[-1] < ma5[-1]
        judgment = judgment and ma5[-1] < ma10[-1]
        if judgment:
            tacticResultData = TacticResultData().setPeriod(self.period).setEventType(
                EventType.TACTIC_MONITOR).setSecurity(self.context.security).setDt(
                self.clock.getDateTime()).setIsInRealtime(self.clock.isInRealTradeTime()).setPrice(
                clo[-1]).setClassify(
                self.noticeclassify).setSourceClassify(self.sourceClassify)
            self.emit(tacticResultData)
            return True

        judgment = ma5[-1] < clo[-1]
        if judgment:
            tacticResultData = TacticResultData().setPeriod(self.period).setEventType(
                EventType.TACTIC_UNMONITOR).setSecurity(self.context.security).setDt(
                self.clock.getDateTime()).setIsInRealtime(self.clock.isInRealTradeTime()).setPrice(clo[-1]).setClassify(
                self.sourceClassify).setSourceClassify(self.noticeclassify)
            self.emit(tacticResultData)
            return True
        return False
